package com.boxin.ims.modules.wechat.busi;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.LotteryRecord;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.LotteryRecordService;
import com.boxin.ims.modules.wechat.service.UserDefineService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;

/**
 * @author Jakemanse
 * @date 2013-9-23
 * @function <p>会员绑定处理器 </p>
 */
@Service
public class MemberBindProcessor extends BusinessProcessor {
	
	
	@Autowired
	UserDefineService userDefineService;
	
	@Autowired
	LotteryRecordService lotteryRecordService;
	
	public MemberBindProcessor(){
		super();
	}
	
	
	
	@Override
	public ProcessMessage process() {
		WechatMember member = getMember();
		String reply =   "您输入姓名和手机号的格式不正确请按照'微会员+申请人姓名+手机号'格式回复,如: 微会员张三130xxxxxxxx,手机号是您的微会员的唯一标识，请正确填,同时我们不会向任何第三方透露您的个人信息，谢谢!";
		if(member == null){	//已是绑定好的会员 
			String upcon = textMessage.getContent();
			if(!upcon.startsWith("微会员")){
				writeToUser(reply);
				return null;
			}else{
				upcon = upcon.replaceAll("微会员", "");
				String regex = "(^.+)(1[3,4,5,8]\\d{9})$";
				Pattern pattern = Pattern.compile(regex);
				
				Matcher matcher =  pattern.matcher(upcon);
				if(matcher.matches()){
					String name = matcher.group(1);
					String mobile = matcher.group(2);
					WechatMember wechatMember = wechatMemberService.getByMobile(user, mobile);
					
					if(wechatMember == null ){		//绑定成功
						wechatMember  = new WechatMember()  ;
						wechatMember.setUser(user);
						wechatMember.setWechatId(openId);
						wechatMember.setScore(0);
						wechatMember.setName(name);
						wechatMember.setMobile(mobile);
						wechatMember.setPassword("");
						wechatMember.setLastSignTime(null);
						wechatMemberService.save(wechatMember);
						reply = "亲，恭喜您成为我们的微会员！您现在可以参与:签到送积分,积分兑换,在线秒杀,在线抽奖,微会员特价等活动。";
						
						//是 否注册会员有礼
						UserDefine userDefine = WechatUtils.getUserDefineByUserId(getUser().getId());
						
						if("1".equals(userDefine.getRegLottery())){	//注册会员有奖
							//TODO 生成注册奖品
							LotteryRecord lotteryRecord = new LotteryRecord();
							lotteryRecord.setSourceType(LotteryRecord.SURCE_TYPE_REG);
							lotteryRecord.setName(userDefine.getLotteryName());
							lotteryRecord.setStartTime(new Date());
							lotteryRecord.setEndTime(DateUtils.getDelayDate(userDefine.getLotteryValidDays()));
							lotteryRecord.setMember(wechatMember);
							lotteryRecord.setStatus(LotteryRecord.STATUS_NORMAL);
							lotteryRecord.setUser(user);
							
							lotteryRecordService.save(lotteryRecord);
							//回复图文
							writeImageToUser(userDefine.getLotteryTitle(), userDefine.getLotteryDesc(), lotteryRecord.getQrCode().getId());
							
						}else{
							writeToUser(reply);
						}
						
						return null;
					}else{
						if(!openId.equals(wechatMember.getWechatId())){
							reply = "您输入的手机号["+mobile+"]已经与其它微信绑定，请确定是否输入错误!";
						}else{
							reply = "亲,您早已是我们的会员了,您现在可以参与:签到送积分,积分兑换,在线秒杀,在线抽奖,微会员特价等活动。";
						}
						
					}
				} 
			}
		} else{
			reply = "亲,您早已是我们的微会员了,您现在可以参与:签到送积分,积分兑换,在线秒杀,在线抽奖,微会员特价等活动。";
		}
		writeToUser(reply);
		return null;
	}


	@Override
	public boolean isNeedBeMember() {
		return false;
	}

}
